#include <config.h>
#include <gio/gio.h>
#include <gtk/gtk.h>
#include <glib/gprintf.h>
#include <glib/gi18n.h>
#ifdef HAVE_X11_XKBLIB_H
#include <X11/Xlib.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <ctype.h>

#include "vcd-main.h"
#include "spicy.h"

#include "vcd-common-api/vcd-download.h"
#include "vcd-common-api/vcd-refresh-vm-info.h"
#include "vcd-common-api/vcd-util.h"
#include "vcd-common-api/vcd-log.h"
#include "vcd-common-api/trace-signals/libdebug.h"
#include "vcd-common-api/vcd-conf/vcd-conf.h"
#include "vcd-linux-ui/vcd-setting-window/setting-main/vcd-sys-upgrade.h"
#include "vcd-linux-ui/vcd-start-window.h"

#if HAVE_GSTAUDIO || HAVE_GSTVIDEO
#include <gst/gst.h>
#endif

const char *__asan_default_options(void) ;
const char *__asan_default_options(void)
{
  return "verbosity=2"
         ":malloc_context_size=100"
         ":strict_string_checks=1"
         ":detect_stack_use_after_return=1"
         ":check_initialization_order=1"
         ":strict_init_order=1"
         ":check_printf=1"
         ":detect_deadlocks=1"
         ":detect_leaks=true"
         ":alloc_dealloc_mismatch=1";
}
static int init_config_network(void)
{
    curl_global_init(CURL_GLOBAL_ALL);


    int download_thread_ret = 0;
    download_thread_ret = start_download_thread();
    g_return_val_if_fail(download_thread_ret == 0, -1);

    int bres = 0;
    bres = read_vcd_conf();
    g_return_val_if_fail(bres == 0, -1);

    M_DEBUG("init success\n");
    return 0;
}

#ifdef __linux__
void waitpidexit(int sig){
    while(waitpid(-1,NULL,WNOHANG) > 0){
        //M_DEBUG("waitpidexit success\n");
    }
}
#endif

static void init_signal(){
    #ifdef __linux__
    signal(SIGCHLD,waitpidexit);
    #endif
}

static void write_version_info_to_log(){
#ifdef PACKAGE_VERSION
  LOG_VERSION("build version:%s",PACKAGE_VERSION);
#endif

#ifdef HAVE_STREAMSINK
  LOG_VERSION("support streamsink HAVE_STREAMSINK:%d",HAVE_STREAMSINK);
#else
  LOG_VERSION("not support streamsink");
#endif

#ifdef HAVE_GSTVIDEO
  LOG_VERSION("support gstreamer HAVE_GSTVIDEO:%d",HAVE_GSTVIDEO);
#else
  LOG_VERSION("not support gstreamer");
#endif

#ifdef HAVE_RECONNECT
  LOG_VERSION("support reconnect HAVE_RECONNECT:%d",HAVE_RECONNECT);
#else
  LOG_VERSION("not support reconnect");
#endif
}

int main(int argc, char *argv[])
{
    #if defined(__linux__) && defined(HAVE_X11_XKBLIB_H)
    Status t = XInitThreads();
     if(t == 0){
        printf("XInitThreads fail:%s\n",strerror(errno));
        return -1;
     }
    #endif
     
    gtk_init(&argc, &argv);
#if HAVE_GSTAUDIO || HAVE_GSTVIDEO
    gst_init(&argc, &argv);
#endif

    debug_backtrace_init();

    init_signal();

    curl_global_init(CURL_GLOBAL_ALL);
    
    int res = vcd_log_init();
    g_return_val_if_fail(res == 0, -1);

    write_version_info_to_log();

    debug_backtrace_init();
    
    if (optionContext(argc, argv)) {       
        M_DEBUG("===============start vcd client gui version:%s, pid:%u===============\n",PACKAGE_VERSION,(unsigned int)getpid());
                
        int download_thread_ret = 0;
        download_thread_ret = start_download_thread();
        g_return_val_if_fail(download_thread_ret == 0, -1);

        int bres = 0;
        bres = read_vcd_conf();
        g_return_val_if_fail(bres == 0, -1);

        vcd_client_start();
    } else {
        M_DEBUG("===============start origin spicy gui=============\n");
        main_origin(argc, argv);
    }


#if HAVE_GSTAUDIO || HAVE_GSTVIDEO
    gst_deinit();
#endif
    return 0;
}
